package org.jeecg.modules.gqHealth.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gqHealth.entity.GqCheckUpRecord;
import org.jeecg.modules.gqHealth.service.IGqCheckUpRecordService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 用户体检记录表
 * @Author: jeecg-boot
 * @Date:   2024-05-12
 * @Version: V1.0
 */
@Api(tags="用户体检记录表")
@RestController
@RequestMapping("/gqHealth/gqCheckUpRecord")
@Slf4j
public class GqCheckUpRecordController extends JeecgController<GqCheckUpRecord, IGqCheckUpRecordService> {
	@Autowired
	private IGqCheckUpRecordService gqCheckUpRecordService;

	/**
	 * 分页列表查询
	 *
	 * @param gqCheckUpRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "用户体检记录表-分页列表查询")
	@ApiOperation(value="用户体检记录表-分页列表查询", notes="用户体检记录表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GqCheckUpRecord>> queryPageList(GqCheckUpRecord gqCheckUpRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GqCheckUpRecord> queryWrapper = QueryGenerator.initQueryWrapper(gqCheckUpRecord, req.getParameterMap());
		Page<GqCheckUpRecord> page = new Page<GqCheckUpRecord>(pageNo, pageSize);
		IPage<GqCheckUpRecord> pageList = gqCheckUpRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param gqCheckUpRecord
	 * @return
	 */
	@AutoLog(value = "用户体检记录表-添加")
	@ApiOperation(value="用户体检记录表-添加", notes="用户体检记录表-添加")
	//@RequiresPermissions("org.jeecg.modules:gq_check_up_record:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody GqCheckUpRecord gqCheckUpRecord) {
		gqCheckUpRecordService.save(gqCheckUpRecord);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param gqCheckUpRecord
	 * @return
	 */
	@AutoLog(value = "用户体检记录表-编辑")
	@ApiOperation(value="用户体检记录表-编辑", notes="用户体检记录表-编辑")
	//@RequiresPermissions("org.jeecg.modules:gq_check_up_record:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody GqCheckUpRecord gqCheckUpRecord) {
		gqCheckUpRecordService.updateById(gqCheckUpRecord);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户体检记录表-通过id删除")
	@ApiOperation(value="用户体检记录表-通过id删除", notes="用户体检记录表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:gq_check_up_record:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		gqCheckUpRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户体检记录表-批量删除")
	@ApiOperation(value="用户体检记录表-批量删除", notes="用户体检记录表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:gq_check_up_record:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gqCheckUpRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户体检记录表-通过id查询")
	@ApiOperation(value="用户体检记录表-通过id查询", notes="用户体检记录表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<GqCheckUpRecord> queryById(@RequestParam(name="id",required=true) String id) {
		GqCheckUpRecord gqCheckUpRecord = gqCheckUpRecordService.getById(id);
		if(gqCheckUpRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(gqCheckUpRecord);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param gqCheckUpRecord
    */
    //@RequiresPermissions("org.jeecg.modules:gq_check_up_record:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GqCheckUpRecord gqCheckUpRecord) {
        return super.exportXls(request, gqCheckUpRecord, GqCheckUpRecord.class, "用户体检记录表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("gq_check_up_record:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GqCheckUpRecord.class);
    }


	 /**
	  *   核销卡信息
	  * @param no
	  * @return
	  */
	 @ApiOperation(value="用户体检记录表-核销卡信息", notes="用户体检记录表-核销卡信息")
	 //@RequiresPermissions("org.jeecg.modules:gq_check_up_record:add")
	 @PostMapping(value = "/checkCard")
	 public Result<String> checkCard(@RequestParam("no") String no) {
		 Integer integer = gqCheckUpRecordService.checkUpCard(no);
		 if (integer == -1){
			 return Result.error("未查询到该卡信息！");
		 }
		 if (integer == -2){
			 return Result.error("卡未激活！");
		 }
		 if (integer == -3){
			 return Result.error("卡已用完！");
		 }

		 if (integer == -4){
			 return Result.error("卡已过期！");
		 }

		 if (integer == -5){
			 return Result.error("账户不是医院！");
		 }
		 if (integer == 1){
			 return Result.OK("核销成功！");
		 }
		 return Result.error("核销异常！");
	 }



	 /**
	  * 医院查询核销记录
	  *
	  * @param startTime
	  * @return
	  */
	 //@AutoLog(value = "用户体检记录表-通过id查询")
	 @ApiOperation(value="用户体检记录表-医院查询核销记录", notes="用户体检记录表-医院查询核销记录")
	 @GetMapping(value = "/hospitalSelectRecord")
	 public Result<?> hospitalSelectRecord( String startTime,String endTime,  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 Page<GqCheckUpRecord> page = new Page<>(pageNo, pageSize);
		 IPage<GqCheckUpRecord> pageList
				  = gqCheckUpRecordService.selectRecord(startTime,endTime, sysUser.getUsername(), page);

		 return Result.OK(pageList);
	 }

	 /**
	  * 医院查询核销记录
	  *
	  * @param
	  * @return
	  */
	 //@AutoLog(value = "用户体检记录表-通过id查询")
	 @ApiOperation(value="用户体检记录表-用户查询体检记录", notes="用户体检记录表-用户查询体检记录")
	 @GetMapping(value = "/userSelectRecord")
	 public Result<?> userSelectRecord( @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 Page<GqCheckUpRecord> page = new Page<GqCheckUpRecord>(pageNo, pageSize);
		 IPage<GqCheckUpRecord> pageList
		  = gqCheckUpRecordService.selectRecord(sysUser.getUsername(), page);

		 return Result.OK(pageList);
	 }

	 /**
	  * 医院查询核销记录
	  *
	  * @param
	  * @return
	  */
	 //@AutoLog(value = "用户体检记录表-通过id查询")
	 @ApiOperation(value="用户体检记录表-管理后台查询", notes="用户体检记录表-管理后台查询")
	 @GetMapping(value = "/selectRecord")
	 public Result<?> selectRecord( String startTime,String endTime,String account,  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 Page<GqCheckUpRecord> page = new Page<GqCheckUpRecord>(pageNo, pageSize);
		 IPage<GqCheckUpRecord> pageList
				 = gqCheckUpRecordService.selectRecord(startTime,endTime, account, page);

		 return Result.OK(pageList);
	 }

}
